package cn.android.support.v7.lib.sin.crown.dialog;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.R;
import cn.android.support.v7.lib.sin.crown.base.BaseApplication;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.model.Single;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import cn.android.support.v7.lib.sin.crown.view.SingleChooses;

/**
 * 男女性别选择器,0男，1女
 * Created by 彭治铭 on 2017/7/12.
 */

public class SingleDialog extends BaseDialog {

    public static SingleDialog singleDialog;

    /**
     * @param activity
     * @param dismiss       true 触摸外区，弹窗会消失。 false 触摸外区不会消失。【返回键都会消失】
     * @param gender        默认选项，0男，1女。传0和1以外的数字，不做选择。
     * @param baseListenner 回调函数(弹窗消失时，且选项发生改变时会回调。如果没改变则不会回调)
     * @return
     */
    public static SingleDialog getInstance(Activity activity, boolean dismiss, int gender, BaseListenner<Integer> baseListenner) {
        if (isfinish(activity,singleDialog)) {
            singleDialog = new SingleDialog(activity);
        }
        singleDialog.gender = gender;
        singleDialog.baseListenner = baseListenner;
        singleDialog.mDismiss = dismiss;
        return singleDialog;
    }

    int gender = 0;//性别 0是男，1是女。
    BaseListenner<Integer> baseListenner;

    public SingleDialog(Activity activity) {
        init(activity, R.layout.crown_dialog_single);
    }

    static View single_dialog;
    SingleChooses singleChooses;

    @Override
    protected void initUI() {
        single_dialog = findViewById(R.id.crown_single_dialog);
        singleChooses = (SingleChooses) findViewById(R.id.singlechooses);

        List<Single> singles = new ArrayList<>();
        singles.add(new Single("0", 0, "男孩"));
        singles.add(new Single("1", 1, "女孩"));
        singleChooses.addSingles(singles);

        single_dialog.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    @Override
    protected void adapterUI() {
        ProportionUtils.getInstance().adapterView(findViewById(R.id.crown_dialog_content));
        ProportionUtils.getInstance().adapterView(single_dialog);
    }

    @Override
    protected void setWindowAnimations() {
        //super.setWindowAnimations();
        window.setWindowAnimations(R.style.crown_window_alpha);
    }

    @Override
    protected boolean isAutoAdapterUI() {
        return false;
    }

    @Override
    protected boolean isStatus() {
        return true;
    }

    @Override
    protected boolean isTransparent() {
        return false;
    }

    @Override
    protected boolean isLocked() {
        return false;
    }

    boolean isCallBack = false;//是否已经回调。
    boolean mDismiss = false;

    @Override
    protected void listener() {
        isCallBack = false;
        if (gender == 0 || gender == 1) {
            singleChooses.selected(gender);//默认选男。
        } else {
            singleChooses.noSelected();
        }
        //选项改变时回调
        singleChooses.setBaseListenner(new BaseListenner<List<Single>>() {
            @Override
            public void onResult(List<Single> result) {
                if (baseListenner != null) {
                    baseListenner.onResult(singleChooses.getSingles().get(0).getScore());
                    isCallBack = true;
                    dismiss();
                }
            }
        });
        findViewById(R.id.crown_dialog_content).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (mDismiss && motionEvent.getAction() == MotionEvent.ACTION_DOWN) {//一定要加ACTION_DOWN判断，不然可能出错。
                    dismiss();
                }
                return true;
            }
        });
    }

    @Override
    protected void recycleView() {
        if (baseListenner != null && !isCallBack && singleChooses.getSingles().size() > 0) {
            if (gender != singleChooses.getSingles().get(0).getScore()) {//选项发生改变时才会回调。
                baseListenner.onResult(singleChooses.getSingles().get(0).getScore());
            }
        }
    }

    public static void setSingleDialog(SingleDialog singleDialog) {
        SingleDialog.singleDialog = singleDialog;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public void setBaseListenner(BaseListenner<Integer> baseListenner) {
        this.baseListenner = baseListenner;
    }

    public static void setSingle_dialog(View single_dialog) {
        SingleDialog.single_dialog = single_dialog;
    }

    public void setSingleChooses(SingleChooses singleChooses) {
        this.singleChooses = singleChooses;
    }

    public void setCallBack(boolean callBack) {
        isCallBack = callBack;
    }

    public void setmDismiss(boolean mDismiss) {
        this.mDismiss = mDismiss;
    }
}
